package com.chrisbanes.photoview;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.View;
import android.widget.ImageView;

import com.lib.smartimage.SmartImageView;

/**
 * 一个可以缩放拖动的 ImageView
 */
public class PhotoView extends SmartImageView {
    private PhotoViewEventHandler mEventHandler;

    @Override
    public void setOnClickListener(View.OnClickListener l) {
        mEventHandler.setOnClickListener(l);
    }

    @Override
    public void setOnLongClickListener(View.OnLongClickListener l) {
        mEventHandler.setOnLongClickListener(l);
    }

    public void setOnMatrixChangeListener(PhotoViewEventHandler.OnMatrixChangedListener listener) {
        mEventHandler.setOnMatrixChangeListener(listener);
    }

    public void setOnPhotoTapListener(PhotoViewEventHandler.OnPhotoTapListener listener) {
        mEventHandler.setOnPhotoTapListener(listener);
    }

    public void setOnOutsidePhotoTapListener(PhotoViewEventHandler.OnOutsidePhotoTapListener listener) {

        mEventHandler.setOnOutsidePhotoTapListener(listener);
    }

    public void setOnDoubleTapListener(GestureDetector.OnDoubleTapListener newOnDoubleTapListener) {

        mEventHandler.setOnDoubleTapListener(newOnDoubleTapListener);
    }

    public void setOnScaleChangeListener(PhotoViewEventHandler.OnScalingListener onScalingListener) {

        mEventHandler.setOnScaleChangeListener(onScalingListener);
    }

    public void setOnSingleFlingListener(PhotoViewEventHandler.OnSingleFlingListener onSingleFlingListener) {

        mEventHandler.setOnSingleFlingListener(onSingleFlingListener);
    }

    public PhotoView(Context context) {

        this(context, null);
    }

    public PhotoView(Context context, AttributeSet attrs) {

        this(context, attrs, 0);
    }

    public PhotoView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        mEventHandler = new PhotoViewEventHandler(this);
        //We always pose as a Matrix scale type, though we can change to another scale type via the attacher
        super.setScaleType(ImageView.ScaleType.MATRIX);
    }

    @Override
    public ImageView.ScaleType getScaleType() {

        return mEventHandler.getScaleType();
    }

    @Override
    public Matrix getImageMatrix() {

        return mEventHandler.getImageMatrix();
    }

    @Override
    public void setScaleType(ImageView.ScaleType scaleType) {
        if (mEventHandler != null) {
            mEventHandler.setScaleType(scaleType);
        }
    }

    @Override
    public void setAdjustViewBounds(boolean adjustViewBounds) {
        /* no op */
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        // setImageBitmap calls through to this method
        if (mEventHandler != null) {
            mEventHandler.update();
        }
    }

    @Override
    public void setImageResource(int resId) {

        super.setImageResource(resId);
        if (mEventHandler != null) {
            mEventHandler.update();
        }
    }

    @Override
    public void setImageURI(Uri uri) {

        super.setImageURI(uri);
        if (mEventHandler != null) {
            mEventHandler.update();
        }
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b) {

        boolean changed = super.setFrame(l, t, r, b);
        mEventHandler.update();
        return changed;
    }

    public void setRotationTo(float rotationDegree) {

        mEventHandler.setRotationTo(rotationDegree);
    }

    public void setRotationBy(float rotationDegree) {

        mEventHandler.setRotationBy(rotationDegree);
    }

    public boolean isZoomEnabled() {

        return mEventHandler.isZoomEnabled();
    }

    public RectF getDisplayRect() {

        return mEventHandler.getDisplayRect();
    }

    public void getDisplayMatrix(Matrix matrix) {

        mEventHandler.getDisplayMatrix(matrix);
    }

    public boolean setDisplayMatrix(Matrix finalRectangle) {

        return mEventHandler.setDisplayMatrix(finalRectangle);
    }

    public float getScale() {

        return mEventHandler.getScale();
    }

    public void setScaleLevels(float minimumScale, float mediumScale, float maximumScale) {

        mEventHandler.setScaleLevels(minimumScale, mediumScale, maximumScale);
    }

    public void setScale(float scale) {

        mEventHandler.setScale(scale);
    }

    public void setScale(float scale, boolean animate) {

        mEventHandler.setScale(scale, animate);
    }

    public void setScale(float scale, float focalX, float focalY, boolean animate) {

        mEventHandler.setScale(scale, focalX, focalY, animate);
    }

    public void setZoomable(boolean zoomable) {

        mEventHandler.setZoomable(zoomable);
    }

    public void setZoomTransitionDuration(int milliseconds) {

        mEventHandler.setZoomTransitionDuration(milliseconds);
    }
}